package com.molichuxing.gateway.bff.dealer.convertor;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.molichuxing.gateway.bff.dealer.entity.request.create.DealerEsignCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.create.FenceCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.FenceModifyVo;
import com.molichuxing.gateway.bff.dealer.entity.response.*;
import com.molichuxing.services.business.dto.request.create.EsignDealerBizCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.FenceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.FenceModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.dealer.entity.request.create.DealerCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.create.StoreCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.DealerModifyVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.StoreModifyVo;
import com.molichuxing.gateway.property.StoreStatusAndVerifyEnum;
import com.molichuxing.services.business.dto.request.create.DealerBizCreateDto;
import com.molichuxing.services.business.dto.request.create.StockBizCreateDto;
import com.molichuxing.services.business.dto.request.create.StoreBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.DealerBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.StockBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.StoreBizModifyDto;
import com.molichuxing.services.business.dto.response.DealerBizDto;
import com.molichuxing.services.business.dto.response.DealerDetailInfoBizDto;
import com.molichuxing.services.business.dto.response.DealerInfoBizDto;
import com.molichuxing.services.business.dto.response.DealerStoreBizDto;
import com.molichuxing.services.business.dto.response.StoreBizDto;
import com.molichuxing.services.business.dto.response.StoreInfoBizDto;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;

/**
 * @author: zhaopenghui
 * @create_date: 2019年08月27日
 **/
public class Convert {

    /*********************************经销商转换开始*************************************************/

    public static List<DealerVo> toDealerVoList(List<DealerBizDto> bizDtoList, Map<Integer, RegionInfoDto> regionInfoMap) {
        List<DealerVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            for (DealerBizDto dealerBizDto : bizDtoList) {
                voList.add(toDealerVo(dealerBizDto, regionInfoMap));
            }
        }
        return voList;
    }

    public static DealerVo toDealerVo(DealerBizDto dealerBizDto, Map<Integer, RegionInfoDto> regionInfoMap) {
        DealerVo dealerVo = new DealerVo();
        if (null == dealerBizDto) {
            return dealerVo;
        }

        BeanUtils.copyProperties(dealerBizDto, dealerVo);
        Integer regionId = dealerBizDto.getRegionId();
        if (null == regionId || null == regionInfoMap || regionInfoMap.size() <= 0) {
            return dealerVo;
        }

        RegionInfoDto regionInfoDto = regionInfoMap.get(regionId);
        if (null == regionInfoDto) {
            return dealerVo;
        }

        String provinceName = regionInfoDto.getProvinceName();
        String cityName = regionInfoDto.getCityName();
        Integer provinceId = regionInfoDto.getProvinceId();
        dealerVo.setProvinceName(provinceName);
        dealerVo.setCityName(cityName);
        dealerVo.setProvinceId(provinceId);
        return dealerVo;
    }

    public static List<DealerListVo> toDealerListVoList(List<DealerBizDto> bizDtoList, Map<Integer, RegionInfoDto> regionInfoMap) {
        List<DealerListVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            for (DealerBizDto dealerBizDto : bizDtoList) {
                voList.add(toDealerListVo(dealerBizDto, regionInfoMap));
            }
        }
        return voList;
    }

    public static DealerListVo toDealerListVo(DealerBizDto dealerBizDto, Map<Integer, RegionInfoDto> regionInfoMap) {
        DealerListVo dealerListVo = new DealerListVo();
        if (null == dealerBizDto) {
            return dealerListVo;
        }

        BeanUtils.copyProperties(dealerBizDto, dealerListVo);
        Integer regionId = dealerBizDto.getRegionId();
        if (null == regionId || null == regionInfoMap || regionInfoMap.size() <= 0) {
            return dealerListVo;
        }

        RegionInfoDto regionInfoDto = regionInfoMap.get(regionId);
        if (null == regionInfoDto) {
            return dealerListVo;
        }

        String provinceName = regionInfoDto.getProvinceFullName();
        String cityName = regionInfoDto.getCityFullName();
        Integer provinceId = regionInfoDto.getProvinceId();
        dealerListVo.setProvinceName(provinceName);
        dealerListVo.setCityName(cityName);
        dealerListVo.setProvinceId(provinceId);
        return dealerListVo;
    }

    public static DealerInfoVo toDealerInfoVo(DealerInfoBizDto dealerInfoBizDto) {
        DealerInfoVo dealerInfoVo = new DealerInfoVo();
        if (dealerInfoBizDto != null) {
            BeanUtils.copyProperties(dealerInfoBizDto, dealerInfoVo);
        }
        return dealerInfoVo;
    }

    public static DealerInfoVo toDealerInfoVo(DealerDetailInfoBizDto dealerDetailInfoBizDto, DealerInfoVo dealerInfoVo) {
        if (dealerDetailInfoBizDto != null) {
            BeanUtils.copyProperties(dealerDetailInfoBizDto, dealerInfoVo);
        }
        return dealerInfoVo;
    }


    public static DealerEsignInfoVo toDealerEsignInfoVo(DealerDetailInfoBizDto dealerDetailInfoBizDto) {
        DealerEsignInfoVo dealerEsignInfoVo = new DealerEsignInfoVo();
        if (dealerDetailInfoBizDto != null) {
            BeanUtils.copyProperties(dealerDetailInfoBizDto, dealerEsignInfoVo);
        }
        return dealerEsignInfoVo;
    }

    public static DealerBizModifyDto toDealerBizModifyDto(DealerModifyVo dealerBizModifyVo) {
        DealerBizModifyDto dealerBizModifyDto = new DealerBizModifyDto();
        if (dealerBizModifyVo != null) {
            BeanUtils.copyProperties(dealerBizModifyVo, dealerBizModifyDto);
        }
        return dealerBizModifyDto;
    }

    public static DealerBizCreateDto toDealerBizCreateDto(DealerCreateVo dealerCreateVo) {
        DealerBizCreateDto dealerBizCreateDto = new DealerBizCreateDto();
        if (dealerBizCreateDto != null) {
            BeanUtils.copyProperties(dealerCreateVo, dealerBizCreateDto);
        }
        return dealerBizCreateDto;
    }


    public static List<DealerStoreVo> toDealerStoreVoList(List<DealerStoreBizDto> dealerStoreList) {
        List<DealerStoreVo> voList = new ArrayList<>();
        if (dealerStoreList != null && dealerStoreList.size() > 0) {
            for (DealerStoreBizDto dealerStoreBizDto : dealerStoreList) {
                voList.add(toDealerStoreVo(dealerStoreBizDto));
            }
        }
        return voList;
    }

    public static DealerStoreVo toDealerStoreVo(DealerStoreBizDto dealerStoreBizDto) {
        DealerStoreVo dealerStoreVo = new DealerStoreVo();
        if (null == dealerStoreBizDto) {
            return dealerStoreVo;
        }

        BeanUtils.copyProperties(dealerStoreBizDto, dealerStoreVo);
        return dealerStoreVo;
    }

    public static List<EsignDealerBizCreateDto> toEsignDealerBizCreateDtoList(List<DealerEsignCreateVo> voList) {
        List<EsignDealerBizCreateDto> dtoList = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (DealerEsignCreateVo dealerEsignCreateVo : voList) {
                dtoList.add(toEsignDealerBizCreateDto(dealerEsignCreateVo));
            }
        }
        return dtoList;
    }

    public static EsignDealerBizCreateDto toEsignDealerBizCreateDto(DealerEsignCreateVo dealerEsignCreateVo) {
        if (null == dealerEsignCreateVo) {
            return null;
        }

        EsignDealerBizCreateDto esignDealerBizCreateDto = new EsignDealerBizCreateDto();
        BeanUtils.copyProperties(dealerEsignCreateVo, esignDealerBizCreateDto);
        return esignDealerBizCreateDto;
    }

    public static List<EsignDealerBizCreateDto> toEsignDealerBizCreateList(List<DealerDetailDto> voList) {
        List<EsignDealerBizCreateDto> dtoList = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (DealerDetailDto dealerDetailDto : voList) {
                dtoList.add(toEsignDealerBizCreateDto(dealerDetailDto));
            }
        }
        return dtoList;
    }

    public static EsignDealerBizCreateDto toEsignDealerBizCreateDto(DealerDetailDto dealerDetailDto) {
        if (null == dealerDetailDto) {
            return null;
        }

        EsignDealerBizCreateDto esignDealerBizCreateDto = new EsignDealerBizCreateDto();
        BeanUtils.copyProperties(dealerDetailDto, esignDealerBizCreateDto);
        return esignDealerBizCreateDto;
    }
    /*********************************经销商转换结束*************************************************/

    /*********************************门店转换开始*************************************************/

    public static List<StoreVo> toStoreVoList(List<StoreBizDto> bizDtoList, Map<Integer, RegionInfoDto> regionInfoMap,
                                              Map<Integer, Integer> carCountMap) {
        List<StoreVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            for (StoreBizDto bizDto : bizDtoList) {
                voList.add(toStoreVo(bizDto, regionInfoMap, carCountMap));
            }
        }
        return voList;
    }

    public static StoreVo toStoreVo(StoreBizDto bizDto, Map<Integer, RegionInfoDto> regionInfoMap,
                                    Map<Integer, Integer> carCountMap) {
        StoreVo storeVo = new StoreVo();
        if (null == bizDto) {
            return storeVo;
        }

        BeanUtils.copyProperties(bizDto, storeVo);
        Integer regionId = bizDto.getRegionId();
        Integer storeId = bizDto.getStoreId();
        if (regionInfoMap != null && regionInfoMap.size() > 0) {
            RegionInfoDto regionInfoDto = regionInfoMap.get(regionId);
            if (regionInfoDto != null) {
                String provinceName = regionInfoDto.getProvinceFullName();
                String cityName = regionInfoDto.getCityFullName();
                Integer provinceId = regionInfoDto.getProvinceId();

                storeVo.setProvinceName(provinceName);
                storeVo.setCityName(cityName);
                storeVo.setProvinceId(provinceId);
            }
        }

        if (carCountMap != null && carCountMap.size() > 0) {
            Integer carCount = carCountMap.get(storeId);
            if (null == carCount) {
                carCount = 0;
            }
            storeVo.setCarCount(carCount);
        } else {
            storeVo.setCarCount(0);
        }

        Integer verifyStatus = bizDto.getVerifyStatus();
        Integer status = bizDto.getStatus();
        String storeStatusStr = getStoreStatusStr(verifyStatus, status);
        storeVo.setStoreStatusStr(storeStatusStr);
        return storeVo;
    }

    private static String getStoreStatusStr(Integer verifyStatus, Integer status) {
        if (StoreVerifyStatusEnum.UN_VERIFY.getValue() == verifyStatus) {
            return StoreStatusAndVerifyEnum.UN_VERIFY.getName();
        } else if (StoreVerifyStatusEnum.VERIFY_REJECT.getValue() == verifyStatus) {
            return StoreStatusAndVerifyEnum.VERIFY_REJECT.getName();
        } else {
            if (StoreStatusEnum.OPEN.getValue() == status) {
                return StoreStatusAndVerifyEnum.OPEN.getName();
            } else if (StoreStatusEnum.SUSPENSION.getValue() == status) {
                return StoreStatusAndVerifyEnum.SUSPENSION.getName();
            }
        }
        return "";
    }

    public static StoreBizModifyDto toStoreBizModifyDto(StoreModifyVo storeModifyVo) {
        StoreBizModifyDto storeBizModifyDto = new StoreBizModifyDto();
        if (storeModifyVo != null) {
            BeanUtils.copyProperties(storeModifyVo, storeBizModifyDto);

            String businessStartTime = storeModifyVo.getBusinessStartTime();
            String busineesEndTime = storeModifyVo.getBusineesEndTime();
            if (StringUtils.isNoneBlank(businessStartTime)) {
                LocalTime startTime = DateUtil.parseTime(businessStartTime);
                storeBizModifyDto.setBusinessStartTime(startTime);
            }
            if (StringUtils.isNoneBlank(busineesEndTime)) {
                LocalTime endTime = DateUtil.parseTime(busineesEndTime);
                storeBizModifyDto.setBusineesEndTime(endTime);
            }
        }
        return storeBizModifyDto;
    }

    public static StockBizModifyDto toStockBizModifyDto(StoreModifyVo storeModifyVo) {
        StockBizModifyDto stockBizModifyDto = new StockBizModifyDto();
        if (storeModifyVo != null) {
            BeanUtils.copyProperties(storeModifyVo, stockBizModifyDto);
        }
        return stockBizModifyDto;
    }

    public static StoreBizCreateDto toStoreBizCreateDto(StoreCreateVo storeCreateVo) {
        StoreBizCreateDto storeBizCreateDto = new StoreBizCreateDto();
        if (storeCreateVo != null) {
            BeanUtils.copyProperties(storeCreateVo, storeBizCreateDto);
            String businessStartTime = storeCreateVo.getBusinessStartTime();
            String busineesEndTime = storeCreateVo.getBusineesEndTime();
            if (StringUtils.isNoneBlank(businessStartTime)) {
                LocalTime startTime = DateUtil.parseTime(businessStartTime);
                storeBizCreateDto.setBusinessStartTime(startTime);
            }
            if (StringUtils.isNoneBlank(busineesEndTime)) {
                LocalTime endTime = DateUtil.parseTime(busineesEndTime);
                storeBizCreateDto.setBusineesEndTime(endTime);
            }
        }
        return storeBizCreateDto;
    }

    public static StockBizCreateDto toStockBizCreateDto(StoreCreateVo storeCreateVo) {
        StockBizCreateDto stockBizCreateDto = new StockBizCreateDto();
        if (storeCreateVo != null) {
            BeanUtils.copyProperties(storeCreateVo, stockBizCreateDto);
        }
        return stockBizCreateDto;
    }

    public static StoreInfoVo toStoreInfoVo(StoreInfoBizDto storeInfoBizDto) {
        StoreInfoVo storeInfoVo = new StoreInfoVo();
        if (storeInfoBizDto != null) {
            BeanUtils.copyProperties(storeInfoBizDto, storeInfoVo);
            Integer verifyStatus = storeInfoBizDto.getVerifyStatus();
            Integer status = storeInfoBizDto.getStatus();
            String storeStatusStr = getStoreStatusStr(verifyStatus, status);
            storeInfoVo.setStoreStatusStr(storeStatusStr);
        }
        return storeInfoVo;
    }

    public static List<DealerGoodsVo> toDealerGoodsVoList(List<DealerBizDto> bizDtoList, Map<Integer, RegionInfoDto> regionInfoMap, Map<Integer, List<GoodsDto>> goodsMap) {
        List<DealerGoodsVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            for (DealerBizDto dealerBizDto : bizDtoList) {
                voList.add(toDealerGoodsVo(dealerBizDto, regionInfoMap, goodsMap));
            }
        }
        return voList;
    }


    public static DealerGoodsVo toDealerGoodsVo(DealerBizDto dealerBizDto, Map<Integer, RegionInfoDto> regionInfoMap, Map<Integer, List<GoodsDto>> goodsMap) {
        DealerGoodsVo dealerVo = new DealerGoodsVo();
        if (null == dealerBizDto) {
            return dealerVo;
        }
        BeanUtils.copyProperties(dealerBizDto, dealerVo);
        RegionInfoDto regionInfoDto = regionInfoMap.get(dealerBizDto.getRegionId());
        if (null != regionInfoDto) {
            dealerVo.setProvinceName(regionInfoDto.getProvinceFullName());
            dealerVo.setCityName(regionInfoDto.getCityFullName());
        }
        List<GoodsDto> goodsList = goodsMap.get(dealerBizDto.getDealerId());
        if (null != goodsList && goodsList.size() > 0) {
            Set<String> goodsTypeSet = new HashSet<>();
            for (GoodsDto goods : goodsList) {
                goodsTypeSet.add(goods.getTypeStr());
            }
            StringBuffer sb = new StringBuffer();
            for (String str : goodsTypeSet) {
                sb.append(str).append("/");
            }
            dealerVo.setGoodsTypeStr(sb.substring(0, sb.length() - 1));
        }
        return dealerVo;
    }

    /********************************* 门店转换结束 *************************************************/

    public static List<DealerProvinceVo> toDealerProvinceVo(List<RegionInfoDto> dtoList) {
        List<DealerProvinceVo> voList = null;
        if (dtoList != null && !dtoList.isEmpty()) {
            voList = new ArrayList<>();
            for (RegionInfoDto beanDto : dtoList) {
                voList.add(toDealerProvinceVo(beanDto));
            }
        }
        return voList;
    }

    public static DealerProvinceVo toDealerProvinceVo(RegionInfoDto beanDto) {
        DealerProvinceVo beanVo = null;
        if (beanDto != null) {
            beanVo = new DealerProvinceVo();
            BeanUtils.copyProperties(beanDto, beanVo);
        }

        return beanVo;
    }

    public static List<DealerVo> toDealerVo(List<DealerDto> dtoList) {
        List<DealerVo> voList = null;
        if (dtoList != null && !dtoList.isEmpty()) {
            voList = new ArrayList<>();
            for (DealerDto beanDto : dtoList) {
                voList.add(toDealerVo(beanDto));
            }
        }
        return voList;
    }

    public static DealerVo toDealerVo(DealerDto beanDto) {
        DealerVo beanVo = null;
        if (beanDto != null) {
            beanVo = new DealerVo();
            BeanUtils.copyProperties(beanDto, beanVo);
        }

        return beanVo;
    }

    public static List<StoreVo> toStoreVo(List<StoreDto> dtoList) {
        List<StoreVo> voList = null;
        if (dtoList != null && !dtoList.isEmpty()) {
            voList = new ArrayList<>();
            for (StoreDto beanDto : dtoList) {
                voList.add(toStoreVo(beanDto));
            }
        }
        return voList;
    }

    public static StoreVo toStoreVo(StoreDto beanDto) {
        StoreVo beanVo = null;
        if (beanDto != null) {
            beanVo = new StoreVo();
            BeanUtils.copyProperties(beanDto, beanVo);
        }

        return beanVo;
    }

    /********************************* 围栏转换结束 *************************************************/

    public static List<FenceVo> toFenceListVo(List<IovFenceDto> dtoList) {
        List<FenceVo> voList = null;
        if (dtoList != null && !dtoList.isEmpty()) {
            voList = new ArrayList<>();
            for (IovFenceDto iovFenceDto : dtoList) {
                voList.add(toFenceVo(iovFenceDto));
            }
        }
        return voList;
    }

    public static FenceVo toFenceVo(IovFenceDto iovFenceDto) {
        FenceVo beanVo = null;
        if (iovFenceDto != null) {
            beanVo = new FenceVo();
            BeanUtils.copyProperties(iovFenceDto, beanVo);
            IovFenceGpsDto iovFenceGpsDto = iovFenceDto.getFenceAreaCoordinateList();
            FenceGpsVo fenceGpsVo = toFenceGpsVo(iovFenceGpsDto);
            beanVo.setFenceAreaCoordinateList(fenceGpsVo);
        }

        return beanVo;
    }

    public static FenceGpsVo toFenceGpsVo(IovFenceGpsDto iovFenceGpsDto) {
        FenceGpsVo beanVo = null;
        if (iovFenceGpsDto != null) {
            beanVo = new FenceGpsVo();
            BeanUtils.copyProperties(iovFenceGpsDto, beanVo);
        }

        return beanVo;
    }

    public static FenceModifyDto toFenceModifyDto(FenceModifyVo fenceModifyVo) {
        FenceModifyDto dto = null;
        if (fenceModifyVo != null) {
            dto = new FenceModifyDto();
            BeanUtils.copyProperties(fenceModifyVo, dto);
        }

        return dto;
    }

    public static FenceCreateDto toFenceCreateDto(FenceCreateVo fenceCreateVo) {
        FenceCreateDto dto = null;
        if (fenceCreateVo != null) {
            dto = new FenceCreateDto();
            BeanUtils.copyProperties(fenceCreateVo, dto);
        }

        return dto;
    }

    /********************************* 围栏转换结束 *************************************************/

}
